library(tidyverse)
Check out these animations:
Animations can create valuable insights, especially for changes over time.
There are lots of packages available to animate graphs in R:
animate- can be used to animate any plot type, written by Yihui Xiegganimate- used to specifically animate ggplot graphics, written by Thomas Lin Pedersenplotly - an interactive plotting library which has animation featuresgooglevis - has a flash based motion chart optiongganimateRecall the gapminder dataset:
library(ggplot2)
theme_set(theme_minimal())
library(gapminder)
head(gapminder)
## # A tibble: 6 x 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 1952 28.8 8425333 779.
## 2 Afghanistan Asia 1957 30.3 9240934 821.
## 3 Afghanistan Asia 1962 32.0 10267083 853.
## 4 Afghanistan Asia 1967 34.0 11537966 836.
## 5 Afghanistan Asia 1972 36.1 13079460 740.
## 6 Afghanistan Asia 1977 38.4 14880372 786.
Let’s start with a static (regular) plot of log(gdp per capita) vs. Life Expectancy in the year 1967.
gapminder %>%
filter(year == 1967) %>%
ggplot(aes(x = gdpPercap,
y=lifeExp,
size = pop)) +
geom_point(show.legend = F, aes(color=continent)) +
scale_color_viridis_d() +
scale_x_log10() +
labs(x = "GDP per capita", y = "Life expectancy")
Wouldn’t it be cool if we could animate this over time? Yes!
library(gganimate)
p<-gapminder %>%
# filter(year == 1967) %>% now we want all years included
ggplot(aes(x = gdpPercap,
y=lifeExp,
size = pop)) +
geom_point(show.legend = F, aes(color=continent)) +
scale_color_viridis_d() +
scale_x_log10() +
labs(x = "GDP per capita", y = "Life expectancy") +
# here we add animation stuff
transition_time(year) +
labs(title = "Year {frame_time}")
#animate(p, fps=1)
You may want to save your plot for use other places…
anim_save("gapminder.gif", animation=p )
ease_aes() is used to “control easing of aesthetics”. It how to points will transition from one frame to the next. Some of the options are:
modifiers include:
For example:
#p + ease_aes("bounce-in")
airquality measures daily air quality measurements in New York, May to September 1973
head(airquality)
## Ozone Solar.R Wind Temp Month Day
## 1 41 190 7.4 67 5 1
## 2 36 118 8.0 72 5 2
## 3 12 149 12.6 74 5 3
## 4 18 313 11.5 62 5 4
## 5 NA NA 14.3 56 5 5
## 6 28 NA 14.9 66 5 6
Let’s consider another static plot:
ggplot(
airquality,
aes(Day, Temp, group = Month, color = factor(Month))) +
geom_line() +
scale_color_viridis_d() +
labs(x = "Day of Month", y = "Temperature") +
theme(legend.position = "top")
Maybe we want to data to gradually appear…
p<- airquality %>%
ggplot(aes(Day, Temp, group = Month, color = factor(Month))) +
geom_line() +
scale_color_viridis_d() +
labs(x = "Day of Month", y = "Temperature") +
theme(legend.position = "top") +
## ADD THIS CODE TO ANIMATE
transition_reveal(Day)
anim_save("airquality.gif", animation=p )
Check out this link for some animated barplots: https://towardsdatascience.com/create-animated-bar-charts-using-r-31d09e5841da
Using gganimate and maps example: https://d4tagirl.com/2017/05/how-to-plot-animated-maps-with-gganimate
Using gganimate to animate Speed Dating Data Example: https://www.statworx.com/ch/blog/animated-plots-using-ggplot-and-gganimate/
A guide to types of transitions: https://gganimate.com/articles/gganimate.html
A full guide to gganimate and all the possible options is available here: https://cran.r-project.org/web/packages/gganimate/gganimate.pdf
plotly and ggplotlylibrary(plotly)
data(gapminder, package = "gapminder")
gg <- ggplot(gapminder,
aes(gdpPercap, lifeExp, color = continent)) +
geom_point(aes(size = pop, frame = year, ids = country)) +
scale_x_log10()
## Warning: Ignoring unknown aesthetics: frame, ids
ggplotly(gg)
You can learn more about plotly and all it’s features here: https://plotly-r.com/index.html